/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sober.sequences;

import com.jspx.utils.NumberUtil;
import com.jspx.utils.DateUtil;
import com.jspx.utils.StringUtil;
import com.jspx.sober.annotation.Id;
import com.jspx.sober.annotation.Column;
import com.jspx.sober.annotation.Table;

import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2004-4-2
 * Time: 12:11:48
 * com.jspx.sequences.Sequences
 */
@Table(name = "jspx_sequences")
public class Sequences implements Serializable
{
    @Id(auto = false)
    @Column(caption = "ID号", length = 80, notNull = true)
    private String name = StringUtil.empty;

    @Column(caption = "当前计数", notNull = true, defaultValue = "0")
    private long keyValue = 0;

    @Column(caption = "最大计数", notNull = true, defaultValue = "0")
    private long keyMax = 99999;

    @Column(caption = "最小计数", notNull = true, defaultValue = "0")
    private int keyMin = 0;

    @Column(caption = "间隔计数", notNull = true, defaultValue = "0")
    private int keyNext = 1;

    @Column(caption = "计数长度", notNull = true, defaultValue = "0")
    private int keyLength = 5;

    @Column(caption = "添加日期", option = "0:否;1:是", notNull = true, defaultValue = "1")
    private int adddate = 0;

    @Column(caption = "日期格式", length=20, notNull = true, defaultValue = "yyMMddhhmmssSS")
    private String fmoatdate = "yyMMddhhmmssSS";

    public Sequences()
    {

    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setKeyValue(int keyValue)
    {
        this.keyValue = keyValue;
    }

    public long getKeyValue()
    {
        return keyValue;
    }

    public void setKeyMax(long keyMax)
    {
        this.keyMax = keyMax;
    }

    public long getKeyMax()
    {
        return keyMax;
    }

    public void setKeyMin(int keyMin)
    {
        this.keyMin = keyMin;
    }

    public int getKeyMin()
    {
        return keyMin;
    }

    public void setKeyNext(int keyNext)
    {
        this.keyNext = keyNext;
    }

    public int getKeyNext()
    {
        return keyNext;
    }

    public void setKeyLength(int keyLength)
    {
        this.keyLength = keyLength;
    }

    public int getKeyLength()
    {
        return keyLength;
    }

    public void setAdddate(int adddate)
    {
        this.adddate = adddate;
    }

    public int getAdddate()
    {
        return adddate;
    }

    public void setFmoatdate(String fmoatdate)
    {
        this.fmoatdate = fmoatdate;
    }

    public String getFmoatdate()
    {
        return fmoatdate;
    }

    public String getNextKey()
    {
        if (keyValue <= keyMin) keyValue = keyMin;
        if (keyValue > keyMax) keyValue = keyMin;
        keyValue = keyValue + keyNext;
        String tmp = NumberUtil.getKeepLength(NumberUtil.toString(keyValue),keyLength);
        if (StringUtil.isNULL(fmoatdate))
        {
            fmoatdate = "yyMMddhhmmssSS";
        }
        if (adddate == 1)
        {
            tmp = DateUtil.toString(fmoatdate) + tmp;
        }
        return tmp;
    }

    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("<sequences value=\"").append(keyValue).append("\" name=\"").append(name).append("\" " + "keyMax=\"").append(keyMax).append("\" keyMin=\"").append(keyMax).append("\" keyNext=\"").append(keyNext).append("\" " + "length=\"").append(keyLength).append("\" fmoatdate=\"").append(fmoatdate).append("\" adddate=\"").append(adddate).append("\"/>\r\n");
        return sb.toString();
    }
}